package rabbit.client.custom;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

public class MyConsumer {
	
	 public static void main(String[] args) throws IOException, TimeoutException {
		//创建链接
		ConnectionFactory factory= new ConnectionFactory();
		factory.setHost("localhost");
		Connection connection = factory.newConnection();
		//建立通道
		Channel channel= connection.createChannel();
		//声明队列
		String queueName ="hello";
		channel.queueDeclare(queueName, false, false , false , null);
		//创建消费者
		Consumer consumer = new DefaultConsumer(channel) {

			@Override
			public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
					throws IOException {
					String message = new String (body,"utf-8") ; 
					System.out.println("收的消息："+message);
			}
			
		};
		//通道执行消费者方法
		channel.basicConsume(queueName,true,consumer) ;
	}
}
